<!--
  ~ This program is part of the OpenLMIS logistics management information system platform software.
  ~
  ~ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  ~  
  ~ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
  ~ You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
  -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <script src="js/jquery-1.11.3.min.js"></script>
    <!--<script src="js/queryScript.js"></script>-->
</head>
<body>
<h1 style="background-color:#E6E6FA;">Electronic Logistics Management Information System( ELMIS )</h1>
<div class="contents">
    <form id="shipmentform">
        <fieldset>
            <legend>Scan the ShipmentID</legend>
            <input type="text" name="shipment_id" id="shipment_id" /> &nbsp;&nbsp;&nbsp;
            <input type="button" value="Scan"  id="scan_sscc"/>
        </fieldset>
    </form>
    <div class="result"></div>
    <p id="message"><img src="../images/ajax-loader.gif" alt=""/></p>
</div>
<script>
    var PACKAGE = {
        items: {},
        current_items: {},
        single_item: {},
        current_shipment_no: "",
        current_lot_number: "",
        current_user: {}
    };


    $(document).ready(function(){

        //getting user details
        $.ajax({
            method: 'GET',
            url: '/user-context',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(data) {
            PACKAGE.current_user = data;
        }).error(function(data){
            alert('error');
        });
        $("#message").hide();
        $("#shipment_id").focus();
        $( "#scan_sscc" ).click(function() {
            //get the current shipment number
            PACKAGE.current_shipment_no = $("#shipment_id").val();

            if(PACKAGE.current_shipment_no != "") {

                $("#message").show();
                $.ajax({
                    method: 'GET',
                    url: '/stock/manufacture/package?filter=shipment_id:eq:' + PACKAGE.current_shipment_no,
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function (data) {
                    if (!data.error) {
                        if (data.manufacture_packages.length == 0) {
                            $(".result").show().html('<h5>There is no package with this package number</h5>');
                            $("#message").hide(slow);
                        } else {
                            PACKAGE.items = data.manufacture_packages;
                            PACKAGE.current_shipment_no = shipment_id;
                            alert('hapa');
                            displayPackageList();
                            $("#message").hide();
                        }
                    }
                }).error(function (data) {
                    alert('error');
                });
            }
        });


    });

    function getShipmentinfo(){
        var html = "<table><tr><td colspan='3'>shipment number: "+PACKAGE.current_shipment_no+"</td> </tr>";
        html += '<tr><td>Scan the Lot Number</td>';
        html += '<td><input type="text" name="lot_number" id="lot_number" /> </td>';
        html += '<td><input type="button" value="Scan"  id="scan_lot"/></td></tr></table>';
        html += "<table border='1' cellpadding='0' cellspacing='0'>";
        html +="<tr>";
        html +="<th>GTIN</th>";
        html +="<th>Item</th>";
        html +="<th>Lot Number</th>";
        html +="<th>Expiry</th>";
        html +="<th>Boxes</th>";
        html +="</tr>";
        $.each(PACKAGE.items, function(index, element) {
            var d = new Date(element.expire_date);
            if(element.delivery_status == "processing"){
                html +="<tr style='background-color: #004444'>";
            }else{
                html +="<tr>";
            }

            html +="<td>"+element.vaccine_packaging.gtin+"</td>";
            html +="<td>"+element.vaccine_packaging.vaccine.name+"</td>";
            html +="<td>"+element.lot_number+"</td>";
            html +="<td>"+ d.toDateString()+"</td>";
            html +="<td>"+parseInt(element.number_of_doses/(element.vaccine_packaging.doses_per_vial*element.vaccine_packaging.vials_per_box))+"</td>";
            html +="</tr>";

        });
        html +="</table>";
        return html;
    }

    function getSpecificLotNumber(){

        var d = new Date(+PACKAGE.single_item.expire_date);
        var boxes = parseInt(PACKAGE.single_item.number_of_doses/(PACKAGE.single_item.vaccine_packaging.doses_per_vial*PACKAGE.single_item.vaccine_packaging.vials_per_box));
        var html = '<table border="1" cellspacing="0" cellpadding="0">';
        html += ' <thead>';
        html += '     <tr><th>GTN</th><th>Item</th><th>Lot</th><th>Expiry</th><th>Vials</th><th>Boxes</th></tr>';
        html += ' </thead>';
        html += '<tr>';
        html += '<td>'+PACKAGE.single_item.vaccine_packaging.gtin+'</td>';
        html += '<td>'+PACKAGE.single_item.vaccine_packaging.vaccine.name+'</td>';
        html += '<td>'+PACKAGE.single_item.lot_number+'</td>';
        html += '<td>'+d.toDateString()+'</td>';
        html += '<td>'+parseInt(PACKAGE.single_item.number_of_doses/PACKAGE.single_item.vaccine_packaging.doses_per_vial)+'</td>';
        html += '<td>'+boxes+'</td>';
        html += '</tr>';
        html += '</table>';

        html += '</br>';
        html += '<table>';
        html += '<tr><td>Quantity Okay</td><td><select id="quantity"><option value="Yes">Yes</option><option value="No">No</option></select></td></tr>';
        html += '<tr><td>Physical Damage</td><td><select id="physical_damage"><option value="Yes">Yes</option><option value="No">No</option></select></td></tr>';
        html += '<tr><td>VVM Status</td><td><select id="vvm_status"><option value="I (Okey)">I (Okey)</option><option value="II (Okey)">II (Okey)</option><option value="III (Bad)">III (Bad)</option><option  value="IV (Bad)">IV (Bad)</option></select></td></tr>';
        html += '<tr><td>Temp monitors Status</td><td><select id="temp_monitor"><option>Good</option><option>Normal</option><option>Bad</option></select></td></tr>';
        html += '<tr><td>Amount Available(boxes)</td><td><input type="text" id="available_boxes" value="'+boxes+'"> </td></tr>';

        html += '</table>';
        html += '</br>';
        html += '<table>';
        html += '<tr>';
        html += '<td>Comments and Observations</td>';
        html += '</tr>';
        html += '<tr>';
        html += '<td><textarea></textarea></td>';
        html += '</tr>';
        html += '</table>';
        html += '</br>';
        html += '<table><tr><td><input type="button" value="Save" id="saveChanges"></td></tr></table>';
        return html;
    }

    function saveOneItem(){
        $("#saveChanges").click(function(){
            var boxes = parseInt(PACKAGE.single_item.number_of_doses/(PACKAGE.single_item.vaccine_packaging.doses_per_vial*PACKAGE.single_item.vaccine_packaging.vials_per_box));
            var arrival = {
                "sscc":                 PACKAGE.single_item.shipment_id,
                "package_number":       PACKAGE.single_item.shipment_id,
                "number_as_expected":   $("#quantity").val(),
                "lot_number":           PACKAGE.single_item.lot_number,
                "gtin":                 PACKAGE.single_item.vaccine_packaging.gtin,
                "number_received":      $("#available_boxes").val(),
                "number_expected":      boxes,
                "physical_damage":      $("#physical_damage").val(),
                "vvm_status":           $("#vvm_status").val(),
                "problems":             $("#").val(),
                "receiveing_user":      PACKAGE.current_user.userId,
                "geographic_zone_id":   PACKAGE.current_user.preferences.DEFAULT_FACILITY,
                "vaccine_packaging_id": PACKAGE.single_item.vaccine_packaging.id
            }
            $.each(PACKAGE.items, function(index, element) {
                if(element.id == PACKAGE.single_item.id){
                    element.arrival_item = arrival;
                    element.delivery_status = "processing";
                }
            });
            console.log(JSON.stringify(PACKAGE.items))
            displayPackageList();
        });
    }

    function displayScan(){
        $("#shipmentform").show('slow');
        $(".result").hide().html("");
        $("#message").hide();
    }

    function displayPackageList(){

        $(".result").hide();
        $(".result").html("");
        var html = getShipmentinfo();
        $(".result").show().html(html);
        $("#shipmentform").hide('slow');

        //processing package items
        $("#lot_number").focus();
        $("#scan_lot").click(function(){
            if($("#lot_number").val() != "") {
                var lot_number = $("#lot_number").val();
                PACKAGE.current_lot_number = lot_number;
                $("#message").show();
                $.ajax({
                    method: 'GET',
                    url: '/stock/manufacture/package?filter=lot_number:eq:'+PACKAGE.current_lot_number,
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function(data) {
                    $("#message").hide();
                    PACKAGE.single_item = data.manufacture_packages[0];
                    var htm = getSpecificLotNumber();
                    $(".result").show().html(htm);
                    saveOneItem();
                }).error(function(data){
                    $("#message").hide();
                    alert('error');
                });
            }
        });
    }


</script>

</body>
</html>
